<?php


// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die();


jimport('joomla.application.component.model');


class ContactFormModelContactForm extends JModel
{
	function __construct()
	{
		parent::__construct();

		global $mainframe;

		$params = $mainframe->getPageParameters('com_contactform');
		$this->_recipient = $params->get('recipient', 'com_contactform@mailinator.com');
		$this->_prefix = $params->get('prefix', '[ContactForm]');
		
		$this->showHomePhone = $params->get('showHomePhone', false);
		$this->showCellPhone = $params->get('showCellPhone', false);
		$this->showCompany = $params->get('showCompany', false);
		$this->showAddress = $params->get('showAddress', false);
		$this->showCity = $params->get('showCity', false);
	}

	var $name = '';
	var $email = '';
	var $subject = '';
	var $message = '';

    function send()
    {
		jimport('joomla.mail.helper');

		$this->name     = JMailHelper::cleanLine(JRequest::getString('name', ''));
		$this->email    = JMailHelper::cleanAddress(JRequest::getString('email', ''));
		$this->subject  = JMailHelper::cleanSubject(JRequest::getString('subject', ''));
		$this->message  = JMailHelper::cleanBody(JRequest::getString('message', ''));

		$this->homePhone = JMailHelper::cleanBody(JRequest::getString('homePhone', ''));
		$this->cellPhone = JMailHelper::cleanBody(JRequest::getString('cellPhone', ''));
		$this->company = JMailHelper::cleanBody(JRequest::getString('company', ''));
		$this->address = JMailHelper::cleanBody(JRequest::getString('address', ''));
		$this->city = JMailHelper::cleanBody(JRequest::getString('city', ''));

		if ('' == $this->name)
		{
			$this->setError(JText::_('CF_INVALID_NAME'));
			return false;
		}
		if (!JMailHelper::isEmailAddress($this->email))
		{
			$this->setError(JText::_('CF_INVALID_EMAIL'));
			return false;
		}
		if ('' == $this->subject)
		{
			$this->setError(JText::_('CF_INVALID_SUBJECT'));
			return false;
		}
		if ('' == $this->message)
		{
			$this->setError(JText::_('CF_INVALID_MESSAGE'));
			return false;
		}

		$body = JText::_('CF_FIELD_NAME') . ': ' . $this->name
			. "\r\n" . JText::_('CF_FIELD_EMAIL') . ': ' . $this->email
			. "\r\n\r\n$this->message\r\n"
			. ($this->showHomePhone ? "\r\n" . JText::_('CF_FIELD_HOME_PHONE') . ': ' . $this->homePhone : '')
			. ($this->showCellPhone ? "\r\n" . JText::_('CF_FIELD_CELL_PHONE') . ': ' . $this->cellPhone : '')
			. ($this->showCompany ? "\r\n" . JText::_('CF_FIELD_COMPANY') . ': ' . $this->company : '')
			. ($this->showAddress ? "\r\n" . JText::_('CF_FIELD_ADDRESS') . ': ' . $this->address : '')
			. ($this->showCity ? "\r\n" . JText::_('CF_FIELD_CITY') . ': ' . $this->city : '')
			;

		jimport('joomla.utilities.utility');

		$result = JUtility::sendMail($this->email, $this->name, $this->_recipient, "$this->_prefix $this->subject",
			$body, false);
		if (JError::isError($result))
		{
			$this->setError($result->message);
			return false;
		}
		return true;
    }

    private $_recipient;
    private $_prefix;
}


?>